package com.qike.baoxiu.service.impl;

import com.qike.baoxiu.controller.NewMessageSocket;
import com.qike.baoxiu.controller.NewOrderSocket;
import com.qike.baoxiu.controller.Socket;
import com.qike.baoxiu.enums.OrderStatusEnums;
import com.qike.baoxiu.mapper.BaoxiuOrderMapper;
import com.qike.baoxiu.mapper.MessageMapper;
import com.qike.baoxiu.pojo.BaoxiuOrder;
import com.qike.baoxiu.pojo.BaoxiuOrderExample;
import com.qike.baoxiu.pojo.Message;
import com.qike.baoxiu.pojo.MessageExample;
import com.qike.baoxiu.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @program: baoxiu
 * @author: Eric
 * @create: 2020-06-09 14:25
 */
@Service
public class MessageService {
    @Autowired
    private Socket socket;
    @Autowired
    private NewOrderSocket newOrderSocket;
    @Autowired
    private NewMessageSocket newMessageSocket;
    @Autowired
    private BaoxiuOrderMapper orderMapper;
    @Autowired
    private MessageMapper messageMapper;

    /**
     * 新订单
     */
    public void sendNewOrder()  {
        BaoxiuOrderExample example = new BaoxiuOrderExample();
        example.createCriteria().andOrderStatusEqualTo(OrderStatusEnums.UN_TAKE.getEcode());
        List<BaoxiuOrder> baoxiuOrders = orderMapper.selectByExample(example);
        if (baoxiuOrders.size()>0){
            try {
                newOrderSocket.sendMessage(Integer.valueOf(baoxiuOrders.size()).toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 统计
     */
    @Async
    public void sendCountOrder(String message) {
//        BaoxiuOrderExample example = new BaoxiuOrderExample();
//        example.createCriteria().andOrderStatusEqualTo(OrderStatusEnums.UN_TAKE.getEcode());
//        List<BaoxiuOrder> baoxiuOrders = orderMapper.selectByExample(example);
//        if (baoxiuOrders.size()>0){
            try {
                socket.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//    }

    /**
     * 统计消息数量
     */
    @Async
    public void sendMessageOrder(Integer uid) {
        MessageExample messageExample = new MessageExample();
        messageExample.createCriteria().andUidEqualTo(uid).andIsReadEqualTo(0);
        List<Message> messages = messageMapper.selectByExample(messageExample);
        try {
            newMessageSocket.sendMessage(Integer.valueOf(messages.size()).toString(),uid);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}